// ActionScript file
//client section
import components.clCredite;
import components.clPrintImeiList;
import components.clPrintPayment;
import components.clPrintPriceList;
import components.downloadWindow;

import flash.events.ContextMenuEvent;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.*;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;

import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.controls.Text;
import mx.controls.TextInput;
import mx.events.CloseEvent;
import mx.events.DataGridEvent;
import mx.events.IndexChangedEvent;
import mx.printing.*;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.StringValidator;

import vclass.Vhttp;

[Bindable]
private var adDgListImeiContextMenu:ContextMenu;

[Bindable]
private var adDgClientiContextMenu:ContextMenu;

[Bindable]
private var adMarcaListCol:XMLListCollection;

[Bindable]
private var adModelListCol:XMLListCollection;

[Bindable]
private var clienti:XMLListCollection;

[Bindable]
private var clienti2:XMLListCollection;//folosita pentru combo 2 la plati ca sa introduc si optiunea de "All"

[Bindable]
private var companii:XMLListCollection;

[Bindable]
private var companii2:XMLListCollection;

private var models:XML;

private function adShowIndex(event:IndexChangedEvent):void{
	var index:Number = event.newIndex;
	switch(index)
	{
		case 0:
			adGetClienti();
			break;
		case 1:
			adGetImei();
			break;
		case 2:
			adGetPriceList();
			break;
		case 3:
			adGetClienti2();
			break;
		case 4:
			adGetPlati();
			adGetClientCredit(adCbClientiPlati.selectedLabel);
			break;
		default:
			return;
			break;
	}
}
//sectiune new imei ###################################################### sectiune new imei ###################################
//se lista cu clientii
private function adGetClienti():void{
	var my:Vhttp = new Vhttp();
	my.target = null;
	my.request.sesiune = sesiune;
	my.request.op = "getClienti";
	my.url = domeniu+"admin2.php";
	my.send();
	function adGetPhonesTmp():void{
		if(alertErrorInvoked)return;
		//confirmareOp(my,false);
		var cl:XML = new XML(my.getXML);
		var clList:XMLList = cl..row;
		var clList1:XMLList = <row label="all" /> + cl..row;
		clienti = new XMLListCollection(clList);
		clienti2 = new XMLListCollection(clList1);
		//adImeiCost afiseaza pretul unui credit selectat
		adGetPhones(adImeiCost);
		adGetCompanie();
		my.removeEventListener(ResultEvent.RESULT,adGetPhonesTmp);
	}
	my.addEventListener(ResultEvent.RESULT,adGetPhonesTmp);
}
//xml structurat cu modelele de telefoane. Functii refereshModels, refreshCompanie ... sunt definite in generalFunctions
private function adGetPhones(myPrice:Text):void{
 	var my:Vhttp = new Vhttp();
	my.target = null;
	my.request.sesiune = sesiune;
	my.request.nume = adCbClienti.selectedLabel;
	my.request.op = "getPhones";
	my.url = domeniu+"admin2.php";
	my.send();
	function initCampuri():void{
		//confirmareOp(my,false);
		
		phones = new XML(my.getXML);
		
		marcaList = phones.marca.@nume;
		marcaListCol = new XMLListCollection(marcaList);
		
		modelList = phones.marca[0].model.@nume;
		modelListCol = new XMLListCollection(modelList);
		
		companieList = phones.marca[0].model[0].companie.@nume;
		companieListCol = new XMLListCollection(companieList);
		
		myPrice.text = phones.marca[0].model[0].companie.@cost+" euro";
		
		my.removeEventListener(ResultEvent.RESULT,initCampuri);
	}
	my.addEventListener(ResultEvent.RESULT,initCampuri);
 }
 private function adSendImei():void{
	if(adImei.text.length < 15){
 		function setFocusToImei():void{
 			adImei.setFocus();
 		}
 		Alert.show("You must have 15 digits for imei!",'Error!',4,this,setFocusToImei);
 		return;
 	}
	var my:Vhttp = new Vhttp();
	my.target = null;
	my.withConfirmare = true;
	my.request.sesiune = sesiune;
	my.request.op = "sendImei";
	my.request.nume = adCbClienti.selectedLabel;
	my.request.marca = adCbMarca.selectedLabel;
	my.request.model = adCbModel.selectedLabel;
	my.request.companie = adCbCompanie.selectedLabel;
	my.request.imei = adImei.text;
	my.request.unlock = adUnlockCode.text;
	my.url = domeniu+"admin2.php";
	my.send();
	function myResult():void{
		//confirmareOp(my);
		my.removeEventListener(ResultEvent.RESULT,myResult);
	}
	my.addEventListener(ResultEvent.RESULT,myResult);
}
private function adScadeCredit():void{
	if(adVal.text == ""){
 		function setFocusToImei():void{
 			adVal.setFocus();
 		}
 		Alert.show("Introduce o valoare!",'Error!',4,this,setFocusToImei);
 		return;
 	}
	var my:Vhttp = new Vhttp();
	my.target = null;
	my.withConfirmare = true;
	my.request.sesiune = sesiune;
	my.request.op = "scadeCredit";
	my.request.val = adVal.text;
	my.request.nume = adCbClienti2.selectedLabel;
	my.url = domeniu+"admin2.php";
	my.send();
	function afisare():void{
		//confirmareOp(my);
		adVal.text = "";
		my.removeEventListener(ResultEvent.RESULT,afisare);
	}
	my.addEventListener(ResultEvent.RESULT,afisare);
}
//sectiune imei list ##################################################### sectiune imei list ##################################
public function adGetImei(imei:String=""):void{
	//parametrul imei se refera la imeiul cautat
 	var my:Vhttp = new Vhttp();
 	my.url = domeniu+"admin2.php";
 	function calculNumar():void{
 			//check result, sa nu am erori sau looged out
 			//confirmareOp(my,false);
 			//acum se calculeaza numarul de itemi
 			adSearchImei.text = "";
			var count:Number = 0;
			for each (var item:Object in adDgListImei.dataProvider){
				count +=1;
			}
			adNumarImeiuri.text = count.toString();
			my.removeEventListener(ResultEvent.RESULT, calculNumar);
	}
	my.target = adDgListImei;
	my.request.sesiune = sesiune;
	my.request.op = "getImeiList";
 	if(imei != ""){
		my.request.imei = adSearchImei.text;
 	}
	my.send();
	my.addEventListener(ResultEvent.RESULT, calculNumar);
 }
 //contex menu pentru stergere imei
 private function adDgListImeiCreateContextMenu():void{
    adDgListImeiContextMenu = new ContextMenu();
    adDgListImeiContextMenu.hideBuiltInItems();
    var menu1Item:ContextMenuItem = new ContextMenuItem('Delete Imei');
    adDgListImeiContextMenu.customItems.push(menu1Item);
    menu1Item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteImei);
}
private function deleteImei(event:ContextMenuEvent):void{
	if(adDgListImei.selectedIndex != -1){
		var my:Vhttp = new Vhttp();
		my.target = null;
		my.withConfirmare = true;
		my.url = domeniu+"admin2.php";
		my.request.sesiune = sesiune;
		my.request.op = "deleteImei";
		my.request.imei = adDgListImei.selectedItem.@imei;
		//required de rpl file, deoarece avem acelasi imei la toate
		//se tine cont de row id la setergerea imeiului
		my.request.id = adDgListImei.dataProvider[adDgListImei.selectedIndex].@id;
		my.send();
		function afisare():void{
			//confirmareOp(my);
			var myObj:Object = adDgListImei.dataProvider;
	 		myObj.removeItemAt(adDgListImei.selectedIndex);
	 		adDgListImei.dataProvider = myObj;
			my.removeEventListener(ResultEvent.RESULT,afisare);
		}
		my.addEventListener(ResultEvent.RESULT,afisare);
 	}else{
 		Alert.show("Selecteaza o înregistrare care doreşti să fie ştearsă!","Eroare");
 	}
}
private function disableEditing(event:DataGridEvent):void {
		//verifica daca celula nu este combo(la ask file). Daca da, se previne editarea
		//event.rowIndex, event.columnIndex
		//event.currentTarget.editedItemRenderer.data[event.dataField]; 
        if(event.itemRenderer.data.@imei == 'ask file')
        {  
            event.preventDefault(); 
        }
}   
private function adImeiEdit(event:DataGridEvent):void{
 		//event.preventDefault();//previne inchiderea editorului
 		var newData:String = TextInput(adDgListImei.itemEditorInstance).text;
 		var oldData:String = event.currentTarget.editedItemRenderer.data[event.dataField];
 		if(event.dataField == '@imei'){
 			adDgListImei.destroyItemEditor();
 			adDgListImei.dataProvider.notifyItemUpdate(adDgListImei.editedItemRenderer);
 			return;
 		}
 		if(event.dataField == '@unlock')
 		{
                if(newData == "") {
                    //nothing to do
                    // Prevent the user from removing focus, and leave the item editor open.
                    //event.preventDefault();
                    // Write a message to the errorString property. This message appears when the user mouses over the editor.
                    //TextInput(adDgListImei.itemEditorInstance).errorString = "Trebuie introdus un cod valid!";
            	}else{
            		adDgListImei.editedItemRenderer.data.@unlock = newData;
            		adDgListImei.destroyItemEditor();
 					adDgListImei.dataProvider.notifyItemUpdate(adDgListImei.editedItemRenderer);
 					//send data to server
 					if(oldData != newData){
	 					var my:Vhttp = new Vhttp();
	 					my.target = null;
	 					my.withConfirmare = true;
	 					my.url = domeniu+"admin2.php";
	 					my.request.sesiune = sesiune;
	 					my.request.op = "sendUnlock";
	 					my.request.id = adDgListImei.dataProvider[adDgListImei.selectedIndex].@id;
	 					my.request.nume = adDgListImei.selectedItem.@nume;
	 					my.request.imei = adDgListImei.selectedItem.@imei;
	 					my.request.unlock = adDgListImei.selectedItem.@unlock;
	 					my.request.cost = adDgListImei.selectedItem.@cost;
	 					my.send();
	 					function afisare():void{
	 						//confirmareOp(my);
	 						my.removeEventListener(ResultEvent.RESULT,afisare);
	 						//remove item from dataGrid
	 						var myObj:Object = adDgListImei.dataProvider;
	 						myObj.removeItemAt(adDgListImei.selectedIndex);
	 						adDgListImei.dataProvider = myObj;
	 					}
	 					my.addEventListener(ResultEvent.RESULT,afisare);
 					}
            	}
 		}
 		if(event.dataField == '@cost'){
                if(newData == "") {
                    // Prevent the user from removing focus, and leave the item editor open.
                    event.preventDefault();
                    // Write a message to the errorString property. This message appears when the user mouses over the editor.
                    TextInput(adDgListImei.itemEditorInstance).errorString = "Trebuie introdus un pret valid!";
            	}else{
            		//verifica daca este un numar
            		adDgListImei.editedItemRenderer.data.@cost = newData;
            		adDgListImei.destroyItemEditor();
 					adDgListImei.dataProvider.notifyItemUpdate(adDgListImei.editedItemRenderer);
            	}
            	return;
 		}
 }
//sectiune credite ####################################################### sectiune credite ####################################
public function adGetPriceList():void{
	 	var my:Vhttp = new Vhttp();
	 	my.target = adDgPriceList;
	 	my.url = domeniu+"admin2.php";
	 	my.request.sesiune = sesiune;
	 	my.request.op = "getPriceList";
	 	my.send();
	 	function populare():void{
	 		//check error sau logged out
	 		if(alertErrorInvoked)return;

	 		adGetMarca();
	 		adGetCompanie();
	 		adGetModels();
	 		my.removeEventListener(ResultEvent.RESULT,populare);
	 	}
	 	my.addEventListener(ResultEvent.RESULT,populare);
 }
 private function adGetMarca():void{
 	var my:Vhttp = new Vhttp();
 	my.target = adCbMarcaCredite;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "getMarca";
 	my.send();
 }
 private function adGetModels():void{
	var my:Vhttp = new Vhttp();
	my.target = null;
	my.request.sesiune = sesiune;
	my.request.op = "getModels";
	my.url = domeniu+"admin2.php";
	my.send();
	function init():void{
		models = new XML(my.getXML);
		var marcaList:XMLList = models.marca.@nume;
		adMarcaListCol = new XMLListCollection(marcaList);
		var modelList:XMLList = modelList = models.marca[0].model.@nume;
		adModelListCol = new XMLListCollection(modelList);
		my.removeEventListener(ResultEvent.RESULT,init);		
	}
	my.addEventListener(ResultEvent.RESULT,init);
}
private function adRefreshModels():void{
	var modelList:XMLList = models.marca.(@nume==adCbMarcaCredite0.selectedLabel).model.@nume;
 	adModelListCol = new XMLListCollection(modelList);
}
 private function adGetCompanie():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "getCompanie";
 	my.send();
 	function tmp():void{
 		var cl:XML = new XML(my.getXML);
		var clList:XMLList = cl..row;
		var clList1:XMLList = <row label="all" /> + cl..row;
		companii = new XMLListCollection(clList);
		companii2 = new XMLListCollection(clList1);
		my.addEventListener(ResultEvent.RESULT,tmp);
 	}
 	my.addEventListener(ResultEvent.RESULT,tmp);
 }
 private function adInsertMarca():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.withConfirmare = true;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.marca = adMarcaCredite.text;
 	my.request.op = "insertMarca";
 	my.send();
 	function afisare():void{
 		var marci:XML = new XML(my.getXML);
 		adCbMarcaCredite.dataProvider = marci..row;
 		adMarcaCredite.text = "";
 		my.removeEventListener(ResultEvent.RESULT,afisare);
 	}
 	my.addEventListener(ResultEvent.RESULT,afisare);
 }
 private function adInsertModel():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.withConfirmare = true;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "insertModel";
 	my.request.marca = adCbMarcaCredite.selectedLabel;
 	my.request.model = adModelCredite.text;
 	my.send();
 	function afisare():void{
 		if(alertErrorInvoked)return;
 		adModelCredite.text = "";
 		adGetModels();
 		my.removeEventListener(ResultEvent.RESULT,afisare);
 	}
 	my.addEventListener(ResultEvent.RESULT,afisare); 	
 }
 private function adInsertCompanie():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.withConfirmare = true;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.companie = adCompanieCredite.text;
 	my.request.op = "insertCompanie";
 	my.send();
 	function repopulare():void{
 		var companii:XML = new XML(my.getXML);
 		adCbCompanieCredite.dataProvider = companii..row;
 		adCompanieCredite.text = "";
 		my.removeEventListener(ResultEvent.RESULT,repopulare);
 	}
 	my.addEventListener(ResultEvent.RESULT,repopulare);
 }
 private function adInsertCredit():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.withConfirmare = true;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "insertCredit";
 	my.request.marca = adCbMarcaCredite0.selectedLabel;
 	my.request.model = adCbModelCredite.selectedLabel;
 	my.request.companie = adCbCompanieCredite.selectedLabel;
 	my.request.cost = adCostCredite.text;
 	my.send();
 	function result():void{
 		if(alertErrorInvoked)return;
 		//repopulare lista preturi
 		var my:Vhttp = new Vhttp();
	 	my.target = adDgPriceList;
	 	my.url = domeniu+"admin2.php";
	 	my.request.sesiune = sesiune;
	 	my.request.op = "getPriceList";
	 	my.send();
	 	my.removeEventListener(ResultEvent.RESULT,result);
 	}
 	my.addEventListener(ResultEvent.RESULT,result);
 }
 private function adPriceEdit(event:DataGridEvent):void{
 	var newData:String = TextInput(adDgPriceList.itemEditorInstance).text;
 	var oldData:String = event.currentTarget.editedItemRenderer.data[event.dataField];
 	if(newData == "") {
            // Prevent the user from removing focus, and leave the item editor open.
            event.preventDefault();
            // Write a message to the errorString property. This message appears when the user mouses over the editor.
            TextInput(adDgPriceList.itemEditorInstance).errorString = "Trebuie introdus un pret valid!";
    	}else{
    		//verifica daca este un numar
    		adDgPriceList.editedItemRenderer.data.@cost = newData;
    		adDgPriceList.destroyItemEditor();
 			adDgPriceList.dataProvider.notifyItemUpdate(adDgPriceList.editedItemRenderer);
 			//send data to server
 			if(oldData != newData){
	 			var my:Vhttp = new Vhttp();
	 			my.target = null;
	 			my.withConfirmare = true;
	 			my.url = domeniu+"admin2.php";
	 			my.request.sesiune = sesiune;
	 			my.request.op = "updatePrice";
	 			my.request.marca = adDgPriceList.selectedItem.@marca;
	 			my.request.model = adDgPriceList.selectedItem.@model;
	 			my.request.companie = adDgPriceList.selectedItem.@companie;
	 			my.request.cost = adDgPriceList.selectedItem.@cost;
	 			my.send();
	 			function afisare():void{
	 				my.removeEventListener(ResultEvent.RESULT,afisare);
	 			}
	 			my.addEventListener(ResultEvent.RESULT,afisare);
 			}
    	} 	
 }
//sectiune clienti ####################################################### sectiune clienti ####################################
//este public deoarece este acesata din componenta clCredite
public function adGetClienti2(nume:String=''):void{
	 	var my:Vhttp = new Vhttp();
	 	my.target = adDgClienti;
	 	my.url = domeniu+"admin2.php";
	 	my.request.sesiune = sesiune;
	 	my.request.op = "getClienti2";
	 	my.request.nume = nume;
	 	my.send();
 }
 private function adDgClientiCreateContextMenu():void{
    adDgClientiContextMenu = new ContextMenu();
    adDgClientiContextMenu.hideBuiltInItems();
    var menu2Item:ContextMenuItem = new ContextMenuItem('Setup Client');
    adDgClientiContextMenu.customItems.push(menu2Item);
    var menu3Item:ContextMenuItem = new ContextMenuItem('Delete Client');
    adDgClientiContextMenu.customItems.push(menu3Item);
    menu2Item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, showPanelCredit);
    menu3Item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteClient);
}
private function showPanelCredit(event:ContextMenuEvent):void{
	if(adDgClienti.selectedIndex != -1){
		var myPanel:clCredite = new clCredite();
		canvas1.addChild(myPanel);
		myPanel.x = canvas1.width/2-myPanel.width/2;
		myPanel.y = canvas1.height/2-myPanel.height/2;
		myPanel.myTitle = "Situatia creditului pentru "+adDgClienti.selectedItem.@nume;
		/*
		function dragIt(event:MouseEvent):void{
			var dragInitiator:clCredite = event.currentTarget as clCredite;
			var dragSource:DragSource = new DragSource();
			//dragSource.addData(value, 'value');
			DragManager.doDrag(dragInitiator, dragSource, event);
		}
		function dragDrop(event:DragEvent):void{
			var dropTarget:Canvas=event.currentTarget as Canvas;
			DragManager.acceptDragDrop(dropTarget);
			/*
			if (event.dragSource.hasFormat('value')) 
                {                    
                    // Accept the drop.
                    DragManager.acceptDragDrop(dropTarget);
                }
		}
		myPanel.addEventListener(MouseEvent.MOUSE_MOVE,dragIt);
		canvas1.addEventListener(DragEvent.DRAG_ENTER,dragDrop);
		*/
		//load data
		myPanel.adNume = adDgClienti.selectedItem.@nume;
		myPanel.loadCredit();
	}else{
		Alert.show("Selecteaza un client!","Eroare");
	}
}

private function deleteClient(event:ContextMenuEvent):void{
	if(adDgClienti.selectedIndex != -1){
		var my:Vhttp = new Vhttp();
		my.target = null;
		my.withConfirmare = true;
		my.url = domeniu+"admin2.php";
		my.request.sesiune = sesiune;
		my.request.op = "deleteClient";
		my.request.nume = adDgClienti.selectedItem.@nume;
		my.send();
		function afisare():void{
			var myObj:Object = adDgClienti.dataProvider;
	 		myObj.removeItemAt(adDgClienti.selectedIndex);
	 		adDgClienti.dataProvider = myObj;
			my.removeEventListener(ResultEvent.RESULT,afisare);
		}
		my.addEventListener(ResultEvent.RESULT,afisare);
 	}else{
 		Alert.show("Selecteaza un client care să fie ştears!","Eroare");
 	}
}
private function adClientiEdit(event:DataGridEvent):void{
 	var newData:String = TextInput(adDgClienti.itemEditorInstance).text;
 	var oldData:String = event.currentTarget.editedItemRenderer.data[event.dataField];
 	var clNume:String = event.dataField;
	if(event.dataField == '@parola'){
 		adDgClienti.editedItemRenderer.data.@parola = newData;
 	}
 	if(event.dataField == '@mail'){
 		adDgClienti.editedItemRenderer.data.@mail = newData;
 	}
 	if(event.dataField == '@phone'){
 		adDgClienti.editedItemRenderer.data.@phone = newData;
 	}
 	adDgClienti.destroyItemEditor();
 	adDgClienti.dataProvider.notifyItemUpdate(adDgClienti.editedItemRenderer);
 	if(oldData != newData){
 		var my:Vhttp = new Vhttp();
 		my.target = null;
 		my.withConfirmare = true;
 		my.url = domeniu+"admin2.php";
 		my.request.sesiune = sesiune;
 		my.request.op = "updateClienti";
 		my.request.nume = adDgClienti.selectedItem.@nume;
 		my.request.parola = adDgClienti.selectedItem.@parola;
 		my.request.mail = adDgClienti.selectedItem.@mail;
 		my.request.phone = adDgClienti.selectedItem.@phone;
 		my.send();
 		function afisare():void{
 			my.removeEventListener(ResultEvent.RESULT,afisare);
 			//referesh combo cu clienti
 			if(clNume == '@nume'){
 				if(alertErrorInvoked)return;
 				adGetClienti();
 			}
 		}
 		my.addEventListener(ResultEvent.RESULT,afisare);
 	}
 }
 private function adInsertClient():void{
 	var my:Vhttp = new Vhttp();
 	my.target = adDgClienti;
 	my.withConfirmare = true;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "insertClient";
 	my.request.nume = adNumeClienti.text;
 	my.request.parola = adParolaClienti.text;
 	my.request.mail = adMailClienti.text;
 	my.request.phone = adPhoneClienti.text;
 	my.request.comment = adComentariuClienti.text;
 	my.request.credit = adCreditClienti.text;
 	my.send();
 	function afisare():void{
 		adNumeClienti.text = "";
 		adParolaClienti.text = "";
 		adMailClienti.text = "";
 		adPhoneClienti.text = "";
 		adCreditClienti.text = "";
 		adComentariuClienti.text = "";
 		my.removeEventListener(ResultEvent.RESULT,afisare);
 	}
 	my.addEventListener(ResultEvent.RESULT,afisare);
 }
//sectiune plati ######################################################### sectiune plati ######################################
private function adGetPlati():void{
 	var my:Vhttp = new Vhttp();
 	my.target = clDgPayment0;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.pp = chkPP.selected;
 	my.request.op = "getPlati";
 	my.request.nume = adCbClientiPlati0.selectedLabel;
 	my.send();
 }
 private function adGetClientCredit(name:String):void{
 	if(adDgClienti.dataProvider != null){
 		for each ( var item:Object in adDgClienti.dataProvider){
 			if(item.@nume == name){
 				adCreditTotalPlati.text = item.@total;
 				adCreditRamasPlati.text = item.@ramas;
 				return;
 			}
 		}
 	}else{
 		//get credit client din server
 		var loader:URLLoader = new URLLoader();
	 	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
	 	var params:URLVariables = new URLVariables();
		params.sesiune = sesiune;
		params.op = "getClientCredit";
		params.nume = name;
	 	var request:URLRequest = new URLRequest(domeniu+"admin2.php");
	 	request.method = URLRequestMethod.POST;
	 	request.data = params;
	 	loader.load(request);
	 	//register listener
	 	function raspuns(event:Event):void{
	 		var flag:String = event.currentTarget.data.flag;
	 		if(flag == '0'){
	 			Alert.show("Eroare la scoaterea creditului clientului");
	 		}else{
	 			adCreditTotalPlati.text = event.currentTarget.data.total;
	 			adCreditRamasPlati.text = event.currentTarget.data.ramas;
	 		}
	 	}
	 	loader.addEventListener(Event.COMPLETE,raspuns);
 		
 	}
 }
 private function updateCredit():void{
 	var my:Vhttp = new Vhttp();
 	my.target = null;
 	my.url = domeniu+"admin2.php";
 	my.request.sesiune = sesiune;
 	my.request.op = "updateCredit";
 	my.request.nume = adCbClientiPlati.selectedLabel;
 	my.request.comment = adComentariuPlati.text;
 	my.request.credit = adCreditPlati.text;
 	my.request.withPP = adChkPlati.selected;
 	my.request.dataEstimata = adDatePlati.text;
 	my.send();
 	function afisare():void{
 		adComentariuPlati.text = "";
 		adCreditPlati.text = "";
 		adChkPlati.selected = false;
 		//confirmareOp(my,false);
 		//modificare afisare credit client
 		adCreditTotalPlati.text = String(Number(adCreditTotalPlati.text)+Number(adCreditPlati.text));
 		adCreditRamasPlati.text = String(Number(adCreditRamasPlati.text)+Number(adCreditPlati.text));
 		my.removeEventListener(ResultEvent.RESULT,afisare);
 		if(alertErrorInvoked)return;
 		adGetPlati();
 	}
 	my.addEventListener(ResultEvent.RESULT,afisare);
 }
 //sectiune upload ######################################################## sectiune upload #####################################
 [Bindable]
 private var fileRef:FileReference = new FileReference();
 
 private function selectHandler(event:Event):void{
 	adListImeiFileName.text = fileRef.name;
 	//pass parametri
 	var params:URLVariables = new URLVariables();
	params.sesiune = sesiune;
 	var request:URLRequest = new URLRequest(domeniu+"uploader2.php");
 	request.method = URLRequestMethod.POST;
	request.data = params;
      try {
         // upload file
          fileRef.upload(request);
      }
      catch (error:Error) {
          Alert.show("error"+error.message);
      }
     fileRef.removeEventListener(Event.SELECT,selectHandler);
 }
 private function uploadCompleteHandler(event:DataEvent):void{
 	adListImeiFileName.text = fileRef.name;
 	var hop:XML = new XML(event.data);
 	var myFlag:String = hop.attribute("flag");
 	var myCode:String = hop.attribute("rpc");
 	switch(myFlag)
 	{
 		case "0":
 			Alert.show("Eroare la upload. Cod "+myCode);
 			break;
 		case "1":
 			Alert.show("Operation succesfull!");
 			//reload dg cu imeiuri
 			adGetImei();
 			break;
 		default:
 			Alert.show("No response from uploader!");
 			break;		
 	}
 	fileRef.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
 }
  private function adUpload():void{
	var excelFilter:FileFilter = new FileFilter("Fisiere excel (*.xls)", "*.xls", "*.asc");
	fileRef.addEventListener(Event.SELECT, selectHandler);
	fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
	//fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    try {
             var success:Boolean = fileRef.browse(new Array(excelFilter));
    }
    catch (error:Error) {
             Alert.show("Unable to browse for files.");
    }
 }
//sectiune download ######################################################### sectiune download(export) file ######################################
 //functie pentru crearea fisierului pe server decideDownload este definita in fisierul generalFunctions
 
 private function adExportImei(userName:String='all',companie:String='all',imeiStatus:String='wait'):void{
 	var loader:URLLoader = new URLLoader();
 	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 	var params:URLVariables = new URLVariables();
	params.sesiune = sesiune;
	params.op = "getImei";
	params.nume = userName;
	params.model = 'all';
	params.companie = companie;
	params.imeiStatus = imeiStatus;
	if(dataStart.text != ''){
		params.dataStart = dataStart.text;
	}
	if(dataEnd.text != ''){
		params.dataEnd = dataEnd.text;
	}
 	var request:URLRequest = new URLRequest(domeniu+"export2.php");
 	request.method = URLRequestMethod.GET;
 	request.data = params;
 	loader.load(request);
 	//register listener
 	loader.addEventListener(Event.COMPLETE,decideDownload);
 }
 private function adExportClienti(userNume:Boolean=true,userPass:Boolean=true,userMail:Boolean=true,userPhone:Boolean=true,userTotal:Boolean=true,userRamas:Boolean=true,userConsumat:Boolean=true):void{
 	var loader:URLLoader = new URLLoader();
 	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 	var params:URLVariables = new URLVariables();
	params.sesiune = sesiune;
	params.op = "getClienti";
	params.nume = userNume;
	params.pass = userPass;
	params.mail = userMail;
	params.phone = userPhone;
	params.total = userTotal;
	params.ramas = userRamas;
	params.consumat = userConsumat;
 	var request:URLRequest = new URLRequest(domeniu+"export2.php");
 	request.method = URLRequestMethod.GET;
 	request.data = params;
 	loader.load(request);
 	//register listener
 	loader.addEventListener(Event.COMPLETE,decideDownload);
 }
 private function adExportPlati(userNume:String='all'):void{
 	var loader:URLLoader = new URLLoader();
 	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 	var params:URLVariables = new URLVariables();
	params.sesiune = sesiune;
	params.op = "getPlati";
	params.nume = userNume;
 	var request:URLRequest = new URLRequest(domeniu+"export2.php");
 	request.method = URLRequestMethod.GET;
 	request.data = params;
 	loader.load(request);
 	//register listener
 	loader.addEventListener(Event.COMPLETE,decideDownload);
 }
 private function adExportListaCredite():void{
 	var loader:URLLoader = new URLLoader();
 	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 	var params:URLVariables = new URLVariables();
	params.sesiune = sesiune;
	params.op = "getCreditList";
	var request:URLRequest = new URLRequest(domeniu+"export2.php");
 	request.method = URLRequestMethod.GET;
 	request.data = params;
 	loader.load(request);
 	//register listener
 	loader.addEventListener(Event.COMPLETE,decideDownload);
 }